package cn.lsh.reducer;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;

public class WordReducer extends Reducer<Text, IntWritable, Text, LongWritable> {
	private LongWritable count = new LongWritable();

	@Override
	protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
		//相同的key为一组，调用一次reduce方法，在方法内迭代这一组数据，进行计算：sum、count、max、min...
		Iterator<IntWritable> iterator = values.iterator();
		long c = 0L;
		while (iterator.hasNext()) {
			c += iterator.next().get();
		}
		count.set(c);
		context.write(key, count);
	}
}
